package toro.ui.event;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MActionListener implements ActionListener
{
    private Class clazz;
    private Object obj;
    private Method action;
    
    public MActionListener(Object obj, String method)
    {
        this.obj = obj;
        this.clazz = obj.getClass();
        Method methods[] = clazz.getMethods();
        for(int i = 0; i < methods.length; i++)
        {
            if(methods[i].getName().equals(method))
            {
                action = methods[i];
                break;
            }
        }
        if(action == null)
        {
            try
            {
                throw new Exception("Method not Found!");
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        
    }
    
    @Override
    public void actionPerformed(ActionEvent e)
    {
        try
        {
            action.invoke(obj, e);
        } catch (IllegalArgumentException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InvocationTargetException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }    


}

/*class foo
{
    public final MActionListener test = new MActionListener(this, "action_foo");
    Entity poop;
    
    public foo()
    {
        poop = new Entity();
        poop.setId(1);
        poop.setName("Poop");
    }
    
    public void action_foo(ActionEvent e)
    {
        System.out.println(poop.getName());
    }
    
    public static void main(String args[])
    {
        JFrame gui = new JFrame();
        JPanel pnl = new JPanel();
        JButton button = new JButton("press me");
        pnl.add(button);
        gui.add(pnl);
        
        foo manager = new foo();
        button.addActionListener(manager.test);
        gui.pack();
        gui.setVisible(true);
        
        
    }
}*/
